<?php
/**
* @filename source_module.php 2013-11-11 02:10:56x
* @author Huming Xu <huming17@126.com>
* @version 1.0.0
* @copyright DZF (c) 2013, Huming Xu
*/

class ctrl_famous_teachers extends famous_teachers{
    public $info_array = array();
	public $page_array = array();
	public $tree_array = array();
    
    static function &instance() {
		static $object;
		if(empty($object)) {
			$object = new self();
		}
		return $object;
	}
    /*
	*	取出一条信息
	*/
	public function do_detail($id='') {
        global $_G;
		$detail = array();
        $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : '';
		$detail = $this->one_info('famous_teachers','teacher_id',$id);
        //DEBUG 获取课程详情
        /*
         * 需求示例：
         * router.get('/teacher/:id', function(req, res) {
            var clist = [
                { type: 1, startTime: '2016-09-01', title: 'C++程序设计', price: 200, school: '嘉兴智慧教育', id: '1', img: '/images/310.png', info: '课程简述', times: '10' },
                { type: 2, startTime: '2016-09-01', title: 'JAVA程序设计', price: 100, school: '嘉兴智慧教育', id: '2', img: '/images/310.png', info: '课程简述', times: '10' },
                { type: 1, startTime: '2016-09-01', title: 'C++程序设计', price: 50, school: '嘉兴智慧教育', id: '3', img: '/images/310.png', info: '课程简述', times: '10' }
            ];
            var id = req.params.id;
            var obj = {
                teacher: {
                    id: '3',
                    name: '沈燕飞',
                    title: '中学语文高级教师',
                    content: '中共党员，嘉兴市第十批、第十一批学科带头人。1997年8月参加工作，担任班主任工作17年，一中实验学校校骨干教师。工作中恪守教师职业道德规范，热爱并尊重学生，自觉遵守学校规章制度。2002、2008、2010、2011、201 ..',
                    img: '/images/20150707_095243_OwxS.png'
                },
                course: clist,
                nav: true
            }
            res.render('about/teacher', obj);
        });
         * 
         */
        //DEBUG 调试输出
        $return = array(
            "clist"=>array(),
            "teacher"=>array()
        );
        $return['teacher']['id']=$detail['teacher_id'];
        $return['teacher']['name']=$detail['teacher_name'];
        $return['teacher']['title']=$detail['brife'];
        $return['teacher']['content']=$detail['information'];
        $return['teacher']['img']=$detail['avatar'];
        //DEBUG 获取教师所有教授课程 暂不分页
        $course_hour = DB::fetch_all("SELECT cm_id FROM ".DB::table('course_hour')." WHERE ch_teacher_id='".$id."' LIMIT 100");
        $course_hour_array = $course_main_array = array();
        foreach($course_hour AS $key => $value){
            $course_hour_array[$value['cm_id']]=$value['cm_id'];
        }
        $page_condition=array(
            'page' => 1,//int 请求页面 页码
            'limit' => 100,//int 每页请求个数
            'perpage' => 100,//int 每页显示个数
            'wheresql' => " AND cm_id IN (".dimplode($course_hour_array).") AND isdelete=0 ",//string //条件SQL语句 
            'orderby' => '',//string 排序规则
        );
        $page_result = $this->api('course_main',$page_condition);
        $course_main_array = array();
        foreach($page_result['page_data'] AS $key => $value){
            $tmp = array("type"=>'',"startTime"=>'',"title"=> '',"price"=> '',"school"=> '',"id"=> '',"img"=> '',"info"=> '',"times"=> '');
            $school_online = $this->one_info('school_online','so_id',$value['so_id']);
            $tmp = array("type"=>$value['cm_cate_id'],"startTime"=>date('Y-m-d',$value['cm_start_time']),"title"=> $value['cm_name'],"price"=> $value['cm_price'],"school"=> $school_online['so_name'],"id"=> $value['cm_id'],"img"=> $value['cm_img'],"info"=>$value['cm_brief'],"times"=> $value['cm_hour_num']);
            $course_main_array[]=$tmp;   
        }
        $return['clist']=$course_main_array;
        if($_G['gp_api']){
            $return['data']=$return;
            $return['code'] = '0';
            echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
        }else{
            $multipage = multi($page_result['total_rows'], $perpage, $page, "index.php?mod=ad&action=info&do=index&keyword=".$keyword);
            include template('famous/teachers/detail');  
        }
	}
    
	/*
	*  添加系统信息
	*/
	public function do_add() {
        global $_G;
		$issubmit = isset($_REQUEST['issubmit']) && !empty($_REQUEST['issubmit']) ? $_REQUEST['issubmit']:'';
		$famous_teachers = isset($_REQUEST['famous_teachers']) && !empty($_REQUEST['famous_teachers']) ? $_REQUEST['famous_teachers']:'';
		if('1'==$issubmit){
            //TODO 后端字段校验 根据具体业务逻辑添加
            //DEBUG 判断是否数组字段,如果是数组转换为逗号间隔 字符串存贮
            foreach($famous_teachers AS $key => $value){
                if(is_array($value) && !empty($value)){
                    $famous_teachers[$key] = implode(",", $value);
                }
            }
            $famous_teachers['create_dateline'] = TIMESTAMP;
            $insert_id = $this->add('famous_teachers',$famous_teachers);
            if($insert_id){
                showmessage('操作成功','index.php?mod=ad&action=info&do=index');
            }else{
                showmessage('操作失败','index.php?mod=ad&action=info&do=index');
            }
		}else{
			include template('famous/teachers/add');
		}
	}
    
	/*
	*  编辑系统信息
	*/
	public function do_edit() {
        global $_G;
		$issubmit = isset($_REQUEST['issubmit']) && !empty($_REQUEST['issubmit']) ? $_REQUEST['issubmit']:'';
		$famous_teachers = isset($_REQUEST['famous_teachers']) && !empty($_REQUEST['famous_teachers']) ? $_REQUEST['famous_teachers']:'';
        $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : '';
        if($id){
            if('1'==$issubmit){
                $famous_teachers['modify_dateline'] = TIMESTAMP;
                //DEBUG 判断是否数组字段,如果是数组转换为逗号间隔 字符串存贮
                foreach($famous_teachers AS $key => $value){
                    if(is_array($value) && !empty($value)){
                        $famous_teachers[$key] = implode(",", $value);
                    }
                }                
                $where = array('id'=>$id);
                $effect_row = $this->edit('famous_teachers',$famous_teachers,$where);
                if($effect_row){
                    showmessage('操作成功','index.php?mod=ad&action=info&do=index');
                }else{
                    showmessage('操作失败','index.php?mod=ad&action=info&do=index');
                }
            }else{
                //DEBUG 获取操作对象信息
                $info = $this->one_info('famous_teachers','id',$id);
                include template('famous/teachers/edit');
            }
        }else{
            showmessage('请选择编辑对象','index.php?mod=ad&action=info&do=index');
        }
	}
    
	/*
	*  删除系统信息
	*/
	public function do_delete() {
        global $_G;
        $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : '';
        if($id){
            $effect_row = $this->delete("famous_teachers", array("id" => $id), $limit=1);
            if($effect_row){
                showmessage('操作成功','index.php?mod=ad&action=info&do=index');
            }else{
                showmessage('操作失败','index.php?mod=ad&action=info&do=index');
            }
        }
    }
	/*
	*  系统信息列表
	*/
	public function do_index(){
        global $_G;
        $page = max(1, intval($_GET['page']));
        $perpage = $limit = isset($_REQUEST['limit']) ? $_REQUEST['limit'] : 10;
        $start=(($page-1) * $perpage);
        $wheresql = "";
        $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';      
        if($keyword){
            $wheresql .= " AND teacher_name LIKE '%".$keyword."%' ";
        }
        $so_id = isset($_REQUEST['so_id']) ? $_REQUEST['so_id'] : 0;
        $wheresql .= " AND so_id = ".$so_id;
        $orderby = " ORDER BY id DESC ";
        //DEBUG 初始化请求请求获取一页列表数据的参数
        $page_condition=array(
            'page' => $page,//int 请求页面 页码
            'limit' => $limit,//int 每页请求个数
            'perpage' => $perpage,//int 每页显示个数
            'wheresql' => $wheresql,//string //条件SQL语句 
            'orderby' => $orderby,//string 排序规则
        );
        //DEBUG 列表数据返回结构
        /*
        $page_result = array(
            //int 返回结果总数
            'total_rows' => $total_rows,
            //array 一页数据数组
            'page_data' => $page_data 
        );
        */
        $page_result = $this->index('famous_teachers',$page_condition);
        foreach($page_result['page_data'] AS $key => $value){
            $tmp = array("id"=>'',"img"=>'',"name"=> '',"title"=> '',"content"=> '');
            $tmp['id']=$value['teacher_id'];
            $tmp['img']=$value['avatar'];
            $tmp['name']=$value['teacher_name'];
            $tmp['title']=$value['brife'];
            $tmp['content']=$value['information'];
            $data['teacher'][] = $tmp;
        }
        $page_result['page_data'] = $data['teacher'];
        //DEBUG 返回输出
        $api=isset($_REQUEST['api']) ? $_REQUEST['api']:'json';
        $client=isset($_REQUEST['client']) ? $_REQUEST['client']:'wap';
        if($_G['gp_api']){
            $return['code'] = '0';
            $return['data']=$page_result;
            echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
        }else{
            $multipage = multi($page_result['total_rows'], $perpage, $page, "index.php?mod=ad&action=info&do=index&keyword=".$keyword);
            include template('famous/teachers/index');   
        }
	}

   /**
     * 接口 AJAX或外部请求检索数据
     *
     * @param $return_type string json/xml.
     *
     * @return print string json/xml.
     */
	public function do_api(){
        $return = '';
        $page = max(1, intval($_GET['page']));
        $perpage = $limit = 20;
        $start=(($page-1) * $perpage);
        $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
        $return_type = isset($_REQUEST['return_type']) ? $_REQUEST['return_type'] : 'json';
        $wheresql = "";
        if($keyword){
            $wheresql = " WHERE title LIKE '%".$keyword."%'";
        }
        $orderby = " ORDER BY id DESC ";
        //DEBUG 初始化请求请求获取一页列表数据的参数
        $page_condition=array(
            'page' => $page,//int 请求页面 页码
            'limit' => $limit,//int 每页请求个数
            'perpage' => $perpage,//int 每页显示个数
            'wheresql' => $wheresql,//string //条件SQL语句 
            'orderby' => $orderby,//string 排序规则
        );
        //DEBUG 列表数据返回结构
        /*
        $page_result = array(
            //int 返回结果总数
            'total_rows' => $total_rows,
            //array 一页数据数组
            'page_data' => $page_data 
        );
        */
        $page_result = $this->api('famous_teachers',$page_condition);      
        if('json'==$return_type){
            $return = json_encode($page_result);	
        }elseif('xml'==$return_type){
            $return = array2xml($page_result);
        }
        //DEBUG 接口响应直接输出返回
		echo $return;
	}
}
?>